package com.xie.smfs.item;

import com.xie.smfs.registry.ModEffects;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.EntityAttributeInstance;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.FoodComponent;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

public class GhostCandyItem extends Item {
    public GhostCandyItem(Settings settings) {
        // 将物品设置为食物，并定义食物属性
        super(settings.food(new FoodComponent.Builder()
                .hunger(2)  // 恢复1点饥饿值（1鸡腿）
                .saturationModifier(0.1f)  // 较低的饱和度
                .alwaysEdible()  // 任何时候都可以食用
                .build()));
    }

    @Override
    public ItemStack finishUsing(ItemStack stack, World world, LivingEntity user) {
        if (!world.isClient && user instanceof PlayerEntity) {
            PlayerEntity player = (PlayerEntity) user;

            // 扣除3点生命上限
            EntityAttributeInstance healthAttribute = player.getAttributeInstance(EntityAttributes.GENERIC_MAX_HEALTH);
            if (healthAttribute != null) {
                double currentMaxHealth = healthAttribute.getBaseValue();
                healthAttribute.setBaseValue(Math.max(1, currentMaxHealth - 3));  // 确保生命值不低于1

                // 如果当前生命值超过新的上限，设置为新上限
                if (player.getHealth() > healthAttribute.getBaseValue()) {
                    player.setHealth((float) healthAttribute.getBaseValue());
                }
            }

            // 给予灵异奔涌效果，持续60秒（1200 ticks）
            player.addStatusEffect(new net.minecraft.entity.effect.StatusEffectInstance(
                    ModEffects.SPIRIT_SURGE,
                    1200,
                    0,
                    false,
                    false
            ));
        }

        // 调用父类方法处理物品消耗
        return super.finishUsing(stack, world, user);
    }
}